package zelix.hack.hacks;

import net.minecraft.client.Minecraft;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.value.Mode;
import zelix.value.ModeValue;
import zelix.value.NumberValue;
import zelix.utils.Wrapper;
import net.minecraft.entity.Entity;
import net.minecraft.init.MobEffects;
import net.minecraft.network.play.client.CPacketPlayer;
import net.minecraft.network.play.client.CPacketUseEntity;
import net.minecraft.network.play.client.CPacketUseEntity.Action;
import zelix.utils.TimerUtils;
import zelix.utils.system.Connection.Side;

public class Criticals extends Hack {

	public ModeValue mode;
	NumberValue delayValue;
	NumberValue hurttime;
	TimerUtils timer = new TimerUtils();
	boolean cancelSomePackets;
	static double attacks = 0;
	Minecraft mc = Wrapper.INSTANCE.mc();

	static boolean antiDesync = false;

	public Criticals() {
		super("Criticals", HackCategory.COMBAT);
		this.mode = new ModeValue("Mode", new Mode("Hyt", true), new Mode("Hyt1", false), new Mode("Packet", false),
				new Mode("Jump", false), new Mode("PJump", false), new Mode("NoGround", false), new Mode("C08P", false),
				new Mode("FunkNight", false) , new Mode("Vulcan" , false) , new Mode("HytPacket" , false) , new Mode("HytPit",false));
		this.delayValue = new NumberValue("Delay", 0D, 0D, 1000D);
		this.hurttime = new NumberValue("HurtTime", 10D, 0D, 10D);
		this.addValue(mode,delayValue,hurttime);

	}

	@Override
	public String getDescription() {
		return "Changes all your hits to critical hits.";
	}

	@Override
	public boolean onPacket(Object packet, Side side) {

		if (Wrapper.INSTANCE.player().onGround) {
			if (side == Side.OUT) {
				if (packet instanceof CPacketUseEntity) {
					CPacketUseEntity attack = (CPacketUseEntity) packet;
					if (attack.getAction() == Action.ATTACK) {
						if (mode.getMode("Packet").isToggled()) {
							if (Wrapper.INSTANCE.player().collidedVertically && this.timer.isDelay(500)) {
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
										Wrapper.INSTANCE.player().posY + 0.0627, Wrapper.INSTANCE.player().posZ,
										false));
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
										Wrapper.INSTANCE.player().posY, Wrapper.INSTANCE.player().posZ, false));
								Entity entity = attack.getEntityFromWorld(Wrapper.INSTANCE.world());
								if (entity != null) {
									Wrapper.INSTANCE.player().onCriticalHit(entity);
								}
								this.timer.setLastMS();
								this.cancelSomePackets = true;
							}
						} else if (mode.getMode("Hyt1").isToggled()) {
							Entity entity = attack.getEntityFromWorld(Wrapper.INSTANCE.world());
							if (entity != null && Wrapper.INSTANCE.player().onGround) {
								Wrapper.INSTANCE.player().onCriticalHit(entity);
							}
						} else if (mode.getMode("Hyt").isToggled()) {
							Entity entity = attack.getEntityFromWorld(Wrapper.INSTANCE.world());
							if (entity != null && Wrapper.INSTANCE.player().onGround) {
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
										Wrapper.INSTANCE.player().posY + 0.0031311231111,
										Wrapper.INSTANCE.player().posZ, false));
								Wrapper.INSTANCE.player().onCriticalHit(entity);
							}

						} else if (mode.getMode("Jump").isToggled()) {
							if (canJump()) {
								Wrapper.INSTANCE.player().jump();
							}
						} else if (mode.getMode("PJump").isToggled()) {
							if (canJump()) {
								Wrapper.INSTANCE.player().jump();
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
										Wrapper.INSTANCE.player().posY + 0.0031311231111,
										Wrapper.INSTANCE.player().posZ, false));

							}
						} else if (mode.getMode("NoGround").isToggled()) {
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer(false));
						} else if (mode.getMode("C08P").isToggled()) {
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.05250000001304, Wrapper.INSTANCE.player().posZ,
									true));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.01400000001304, Wrapper.INSTANCE.player().posZ,
									false));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.00150000001304, Wrapper.INSTANCE.player().posZ,
									false));
						} else if (mode.getMode("FunkNight").isToggled()) {
							if(Wrapper.INSTANCE.player().hurtTime>hurttime.getValue()&&timer.hasReached(delayValue.getValue().floatValue())) {
								
							
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.051400040018, Wrapper.INSTANCE.player().posZ,
									true));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.0015000018, Wrapper.INSTANCE.player().posZ,
									false));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.01400000018, Wrapper.INSTANCE.player().posZ,
									false));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(Wrapper.INSTANCE.player().posX,
									Wrapper.INSTANCE.player().posY + 0.0015300600018, Wrapper.INSTANCE.player().posZ,
									false));
							timer.reset();
							
							}
						} else if (mode.getMode("HytPacket").isToggled()) {
							Entity entity = attack.getEntityFromWorld(Wrapper.INSTANCE.world());
							double x =  mc.player.posX , y = mc.player.posY , z = mc.player.posZ;
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(x , y + 0.11, z ,true));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(x , y + 0.1100013579, z ,false));
							Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(x, y + 0.0000013579, z, false));
							mc.player.onCriticalHit(entity);
						}else if (mode.getMode("HytPit").isToggled()){
							double x =  mc.player.posX , y = mc.player.posY , z = mc.player.posZ;
							attacks++;
							if (attacks > 0){
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(x, y + 0.2, z, false));
								Wrapper.INSTANCE.sendPacket(new CPacketPlayer.Position(x, y + 0.1216, z, false));
								attacks = 0;
							}else {
								antiDesync = false;
							}
						}
					}
				} else if (mode.getMode("Packet").isToggled() && packet instanceof CPacketPlayer) {
					if (cancelSomePackets) {
						cancelSomePackets = false;
						return false;
					}
				}
			}
		}
		return true;
	}

	boolean canJump() {
		if (Wrapper.INSTANCE.player().isOnLadder()) {
			return false;
		}
		if (Wrapper.INSTANCE.player().isInWater()) {
			return false;
		}
		if (Wrapper.INSTANCE.player().isInLava()) {
			return false;
		}
		if (Wrapper.INSTANCE.player().isSneaking()) {
			return false;
		}
		if (Wrapper.INSTANCE.player().isRiding()) {
			return false;
		}
		if (Wrapper.INSTANCE.player().isPotionActive(MobEffects.BLINDNESS)) {
			return false;
		}
		return true;
	}
}
